package com.murongxue.blog.model;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class Article {
    public Integer aid;
    public Integer uid;
    public String title;
    public String type;
    public String content;
    public LocalDate publishedAt;

    public Article() {}

    public Article(int aid, int uid, String title, String type, String content, LocalDate publishedAt) {
        this.aid = aid;
        this.uid = uid;
        this.title = title;
        this.type = type;
        this.content = content;
        this.publishedAt = publishedAt;
    }

    public String getPublishedAt() {
        // 按照 2022-01-08
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        return publishedAt.format(formatter);
    }

    public String getDesc() {
        // 从正文中截取前 100 个字符，作为文章的简要描述
        // 如果长度不够 100，以文章长度为准
        int length = Integer.min(content.length(), 100);
        return content.substring(0, length);
    }
}
